package com.client.view;

import com.client.bean.UserInfo;
import com.client.db.UserDbOper;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;

/**
 * Created by Administrator on 2016/12/16.
 */
public class RegUser extends JFrame implements ActionListener{
	private UserInfo userInfo;
	private JLabel bgimage;
	private JComboBox cbface,nation,star;
	private JTextField code,nickName,port,age,remark,ipAddr;
	private JRadioButton sex1,sex2;
	private JTextArea information;
	private JPasswordField password,fgpassword;
	private  JButton btnSave,btnClose;
	private String nations[] = {"汉","满","回","壮","藏"};
	private String stars[] = {"水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座","摩羯座"};
	private String faces[] = {"face/0.png", "face/1.png", "face/2.png", "face/3.png","face/4.png", "face/5.png", "face/6.png", "face/7.png", "face/8.png", "face/9.png"};

	private boolean m_badd = true;
	private MainView mainview = null;

	/**
	 * 注册用
	 */
	public RegUser() {
		m_badd = true;
		init();
	}

	/**
	 * 修改用
	 * @param userInfo
	 */
	public RegUser(UserInfo userInfo , MainView mainview){
		this.userInfo = userInfo;
		this.mainview = mainview;//主窗口
		m_badd = false;
		init();
		code.setText(userInfo.getCode() + "");
		nickName.setText(userInfo.getNicknName());
		for (int i = 0; i < faces.length; i++) {
			if (userInfo.getFace().equals(faces[i])) {
				cbface.setSelectedIndex(i);
				break;
			}
		}
		port.setText(userInfo.getPort() + "");
		password.setEditable(false);
		fgpassword.setEditable(false);
		ipAddr.setEditable(false);
		port.setEditable(false);
		age.setText(userInfo.getAge() + "");

		if (userInfo.getSex().equals("男")) {
			sex1.setSelected(true);
		} else
			sex2.setSelected(true);

		for (int i = 0; i < nations.length; i++) {
			if (userInfo.getNation().equals(nations[i])) {
				nation.setSelectedIndex(i);
				break;
			}
		}

		for (int i = 0; i < stars.length; i++) {
			if (userInfo.getStar().equals(stars[i])) {
				star.setSelectedIndex(i);
				break;
			}
		}
		remark.setText(userInfo.getRemark());
		information.setText(userInfo.getInformation());

	}

	public void init() {

		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}

		Icon bg_img=new ImageIcon("images/main_back.jpg");
		bgimage=new JLabel(bg_img);
		add(bgimage);
		JLabel lblTitle = new JLabel("个人资料");
		lblTitle.setFont(new Font("华文新魏",Font.BOLD,28));
		lblTitle.setBounds(150,30,150,40);
		bgimage.add(lblTitle);
		bgimage.setBorder(BorderFactory.createBevelBorder(2));
		int i = 0;
		Icon[] face = {
				new ImageIcon(faces[i++]),
				new ImageIcon(faces[i++]),
				new ImageIcon(faces[i++]),
				new ImageIcon(faces[i++]),
				new ImageIcon(faces[i++]),
				new ImageIcon(faces[i++]),
				new ImageIcon(faces[i++]),
				new ImageIcon(faces[i++]),
				new ImageIcon(faces[i++]),
				new ImageIcon(faces[i++]),
		};

		cbface = new JComboBox(face);
		cbface.setMaximumRowCount(5);
		JLabel lblface = new JLabel("头像",JLabel.RIGHT);
		lblface.setBounds(270,70,60,20);
		cbface.setBounds(330,70,90,65);
		bgimage.add(lblface);
		bgimage.add(cbface);

		JLabel lblchatcode = new JLabel("帐号: ",JLabel.RIGHT);
		JLabel lblnickName = new JLabel("昵称: ",JLabel.RIGHT);
		JLabel lblpassword = new JLabel("密码: ",JLabel.RIGHT);
		JLabel lblfgpassword = new JLabel("确认密码: ",JLabel.RIGHT);
		JLabel lblipAddr = new JLabel("ip地址: ",JLabel.RIGHT);
		JLabel lblport = new JLabel("连接端口: ",JLabel.RIGHT);
		JLabel lblage = new JLabel("年龄: ",JLabel.RIGHT);
		JLabel lblsex = new JLabel("性别: ",JLabel.RIGHT);
		JLabel lblnation= new JLabel("民族: ",JLabel.RIGHT);
		JLabel lblstar= new JLabel("星座: ",JLabel.RIGHT);
		JLabel lblremark= new JLabel("备注: ",JLabel.RIGHT);
		JLabel lblinformation = new JLabel("信息: ",JLabel.RIGHT);

		code = new JTextField(10);
		lblchatcode.setBounds(50,70,60,25);
		code.setBounds(110,70,100,25);
		bgimage.add(lblchatcode);
		bgimage.add(code);

		nickName = new JTextField(15);
		lblnickName.setBounds(50,110,60,25);
		nickName.setBounds(110,110,100,25);
		bgimage.add(lblnickName);
		bgimage.add(nickName);

		password = new JPasswordField(15);
		fgpassword = new JPasswordField(15);
		lblpassword.setBounds(50,150,60,25);
		lblfgpassword.setBounds(270,150,60,25);
		password.setBounds(110,150,100,25);
		fgpassword.setBounds(330,150,100,25);
		bgimage.add(lblpassword);
		bgimage.add(password);
		bgimage.add(lblfgpassword);
		bgimage.add(fgpassword);

		InetAddress addr = null;
		try {
			addr = InetAddress.getLocalHost();
		} catch (Exception e){
			e.printStackTrace();
		}

		ipAddr = new JTextField(addr.getHostAddress());
		lblipAddr.setBounds(50,190,60,30);
		ipAddr.setBounds(110,190,100,30);
		ipAddr.setEditable(false);//设为只读
		bgimage.add(lblipAddr);
		bgimage.add(ipAddr);

		port = new JTextField();
		lblport.setBounds(270,190,60,25);
		port.setBounds(330,190,100,25);
		bgimage.add(lblport);
		bgimage.add(port);

		age = new JTextField("0");
		lblage.setBounds(50,230,60,25);
		age.setBounds(110,230,60,25);
		bgimage.add(lblage);
		bgimage.add(age);

		sex1 = new JRadioButton("男");
		sex2 = new JRadioButton("女");
		sex1.setSelected(true);
		lblsex.setBounds(270,230,60,25);
		sex1.setBounds(330,230,60,25);
		sex2.setBounds(390,230,60,25);
		ButtonGroup bg = new ButtonGroup();
		bg.add(sex1);
		bg.add(sex2);
		bgimage.add(lblsex);
		bgimage.add(sex1);
		bgimage.add(sex2);

		nation = new JComboBox(nations);
		lblnation.setBounds(50,270,60,25);
		nation.setBounds(110,270,100,25);
		bgimage.add(lblnation);
		bgimage.add(nation);

		star = new JComboBox(stars);
		lblstar.setBounds(270,270,60,25);
		star.setBounds(330,270,100,25);
		bgimage.add(lblstar);
		bgimage.add(star);

		remark = new JTextField();
		lblremark.setBounds(50,310,60,25);
		remark.setBounds(110,310,320,25);
		bgimage.add(lblremark);
		bgimage.add(remark);

		information = new JTextArea();
		information.setBounds(110,350,320,80);
		lblinformation.setBounds(50,350,60,25);
		JScrollPane spanel = new JScrollPane(information);
		spanel.setBounds(110,350,320,80);
		bgimage.add(lblinformation);
		bgimage.add(spanel);

		btnSave = new JButton("保存");
		btnClose = new JButton("关闭");
		btnSave.setBounds(150,450,80,30);
		btnClose.setBounds(230,450,80,30);
		bgimage.add(btnSave);
		bgimage.add(btnClose);
		btnSave.addActionListener(this);
		btnClose.addActionListener(this);

		setResizable(false);
		setSize(500, 600);
		setVisible(true);
		setLocationRelativeTo(null);
//		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

	}

	public static void main(String[] args) {
		new RegUser();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnSave) {
			UserInfo userInfo = new UserInfo();
			try {
				userInfo.setCode(Integer.parseInt(code.getText()));
			} catch (Exception ex) {
				JOptionPane.showMessageDialog(null,"帐号不合法");
			}
			//验证密码和确认密码是否一致
			if (new String(password.getPassword()) == ""){
				JOptionPane.showMessageDialog(null,"未输入密码，无法注册","提示信息",JOptionPane.ERROR_MESSAGE);
				return;
			}
			if (!new String(password.getPassword()).equals(new String(fgpassword.getPassword()))){
				JOptionPane.showMessageDialog(null,"密码与验证密码不一致，无法注册","提示信息",JOptionPane.ERROR_MESSAGE);
				return;
			}

			if (nickName.getText() == "") {
				JOptionPane.showMessageDialog(null,"昵称不能为空","提示信息",JOptionPane.ERROR_MESSAGE);
				return;
			}

			int nport = 0;
			try {
				nport = Integer.parseInt(port.getText().trim());
			} catch (Exception ek) {
				JOptionPane.showMessageDialog(null,"端口值不合法","提示信息",JOptionPane.ERROR_MESSAGE);
				return;
			}

			if (nport < 1024 || nport > 65535) {
				JOptionPane.showMessageDialog(null,"端口必须在1024-65535之间","提示信息",JOptionPane.ERROR_MESSAGE);
				return;
			}

			int nage = 0;
			try {
				nage = Integer.parseInt(age.getText().trim());
			} catch (Exception ej) {
				JOptionPane.showMessageDialog(null,"年龄不合法","提示信息",JOptionPane.ERROR_MESSAGE);
				return;
			}
			userInfo.setCode(Integer.parseInt(code.getText().trim()));
			userInfo.setNicknName(nickName.getText().trim());
			userInfo.setPassword(new String(password.getPassword()));
			userInfo.setIpAddr(ipAddr.getText().trim());
			userInfo.setPort(Integer.parseInt(port.getText().trim()));
			userInfo.setFace(faces[cbface.getSelectedIndex()]);
			userInfo.setAge(Integer.parseInt(age.getText().trim()));
			if (sex1.isSelected()) {
				userInfo.setSex("男");
			} else {
				userInfo.setSex("女");
			}
			userInfo.setNation(nations[nation.getSelectedIndex()]);
			userInfo.setStar(stars[star.getSelectedIndex()]);
			userInfo.setRemark(remark.getText().trim());
			userInfo.setInformation(information.getText().trim());
			userInfo.setStatus(0);

			UserDbOper db = new UserDbOper();
			//判断端口是否被占用
			boolean b = db.isPort(userInfo.getPort());
			if (b && m_badd) {
				JOptionPane.showMessageDialog(null,"端口已被占用","提示信息",JOptionPane.ERROR_MESSAGE);
				port.requestFocus();
			}
			if (m_badd) //新增
				b = db.addUser(userInfo);
			else//修改
				b = db.updateUserInfo(userInfo);

			if (b && m_badd) {
				JOptionPane.showMessageDialog(null,"注册成功","提示信息",JOptionPane.ERROR_MESSAGE);
				return;
			} else if (!b && m_badd){
				JOptionPane.showMessageDialog(null,"注册失败","提示信息",JOptionPane.ERROR_MESSAGE);
				return;
			}

			if (b && !m_badd) {
				JOptionPane.showMessageDialog(null,"修改成功","提示信息",JOptionPane.ERROR_MESSAGE);
				mainview.userInfo = userInfo;//实时更新
				//获取昵称，备注，qq号码，个性签名
				String str = "";
				if (userInfo.getRemark() != null && !userInfo.getRemark().equals("")) {
					str = userInfo.getRemark() + "(";
				} else {
					str = userInfo.getNicknName() + "(";
				}
				str += userInfo.getCode() + ")," + userInfo.getInformation();
//				mainview.lblhead = new JLabel(str, new ImageIcon(userInfo.getFace()), JLabel.LEFT);
				mainview.lblhead.setIcon(new ImageIcon(userInfo.getFace()));
				mainview.lblhead.setText(str);
				dispose();

			} else if (!b && !m_badd){
				JOptionPane.showMessageDialog(null,"修改失败","提示信息",JOptionPane.ERROR_MESSAGE);
				return;
			}
		} else if (e.getSource() == btnClose) {
//			System.exit(0);
			dispose();
		}
	}
}
